#import "SEGWinningPublicTopView.h"

@implementation SEGWinningPublicTopViewModel
-(instancetype)init{
    if (self == [super init]) {
        _title = @"";
        _bannerUrlString = @"";
    }
    return self;
}
@end

@implementation SEGWinningPublicTopViewLayout
- (instancetype)initWithData:(SEGWinningPublicTopViewModel *)data type:(SEGEnumWinningPublicTopViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segWinningPublicTopViewLayout];
    return self;
}

-(void)segWinningPublicTopViewLayout{
    //高度固定
    _totalHeight = kWinningPublicTopViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _titleLabelHeight = 0;
    _titleLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
}

//数据处理
-(void)layout{
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:16];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_data.title];
    text.yy_font = font;
    text.yy_color = kColor_1;

    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(LYScreenWidth(), 44);
    container.maximumNumberOfRows = 1;
    
    _titleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
}
@end

@implementation SEGWinningPublicTopView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kWinningPublicTopViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.bannerImageView];
    [self addSubview:self.titleLabel];
    
    /*
     self.userInteractionEnabled = YES;
     __weak typeof(self) _self = self;
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
     [SEGAlertView showTmepMessageWithMessage:@"SEGWinningPublicTopViewClick"];
     }];
     [self addGestureRecognizer:tap];
     */
    self.backgroundColor = kColor_W;
    return self;
}

-(void)setWithLayout:(SEGWinningPublicTopViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;

    [self.bannerImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,layout.data.bannerUrlString]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
    
    self.titleLabel.textLayout = layout.titleLabelTextLayout;
    
    [self setupUI];
}

//界面布局
-(void)setupUI{

    [self.bannerImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(187.0);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bannerImageView.mas_bottom);
        make.left.equalTo(self).mas_offset(15);
        make.right.equalTo(self).mas_offset(-15);
        make.height.mas_equalTo(44.0);
    }];
    
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
}

#pragma mark - getter
-(UIImageView *)bannerImageView{
    if (!_bannerImageView) {
        _bannerImageView = [[UIImageView alloc]init];
//        _bannerImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _bannerImageView.contentMode = UIViewContentModeScaleAspectFill;
        _bannerImageView.layer.masksToBounds = YES;
        //_bannerImageView.layer.cornerRadius = 0.5;
        //if (kDebug)_bannerImageView.backgroundColor = kSegRandomColor;
    }
    return _bannerImageView;
}


-(SEGLabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[SEGLabel alloc]init];
        _titleLabel.userInteractionEnabled = NO;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _titleLabel.displaysAsynchronously = YES;
        _titleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _titleLabel.fadeOnHighlight = NO;
        _titleLabel.numberOfLines = 0;
        _titleLabel.fadeOnAsynchronouslyDisplay = NO;
        _titleLabel.fadeOnAsynchronouslyDisplay = NO;
        _titleLabel.backgroundColor = kColor_W;
        //__weak typeof(self) _self = self;
        //_titleLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //[SEGAlertView showTmepMessageWithMessage:@"_titleLabelClick"];
        //if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocoltitleLabel:)]) {
        //[_self.delegate SEGprotocoltitleLabel:_self.titleLabel];
        //}
        //};
    }
    return _titleLabel;
}
@end
